package cn.lic.tool.poi.excel.function;

import java.util.Objects;

import com.alibaba.fastjson.JSON;

/**
 * 引用变量
 * @author Administrator
 *
 */
public class RValue extends IValue {
	
	public RValue(Object value) {
		this.value = value;
	}
	
	@Override
	public <T> T castValue(Type type) {
		if  (value == null) {
			return null;
		}
		context = Objects.requireNonNull(context, "Context is required for Rvalue");
		String key = String.valueOf(value);
		
		Object val = null;
		switch (type) {
			case OBJECT:
				val = context.get(key);
			break;
			case INT:
				val = context.getInteger(key);
				break;
			case FLOAT:
				val = context.getFloat(key);
				break;
			case STRING:
				val = context.getString(key);
				break;
			case BOOL:
				val = context.getBoolean(key);
			case BIG_DECIMAL:
				val = context.getBigDecimal(key);
				break;
			case JSON_OBJECT:
				val = context.getJSONObject(key);
				break;
			case JSON_ARRAY:
				val = context.getJSONArray(key);
				break;
		default:
			val = value;
		}
		return (T)val;
	}
	@Override
	public boolean isNull() {
		context = Objects.requireNonNull(context, "Context is required for Rvalue");
		return this.value != null && context.containsKey(String.valueOf(this.value));
	}

	@Override
	public String toString() {
		return "R:" + JSON.toJSONString(value);
	}
}
